package com.osfw.system.entity.manage;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.osfw.framework.core.entity.BaseEntity;
import com.osfw.framework.xss.Xss;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

/**
 * 用户对象 sys_user
 */
@Data
@Accessors(chain = true)
public class SysUser extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /* 用户ID */
    @TableId(value = "user_id_", type = IdType.ASSIGN_ID)
    private Long userId;

    /* 部门ID */
    @TableField(value = "dept_id_")
    private Long deptId;

    /* 部门名称 */
    @TableField(exist = false)
    private String deptName;

    /* 部门父ID */
    @TableField(exist = false)
    private Long parentId;

    /* 角色ID */
    @TableField(exist = false)
    private Long roleId;

    /* 登录名称 */
    @Xss(message = "登录账号不能包含脚本字符")
    @NotBlank(message = "登录账号不能为空")
    @Size(min = 0, max = 30, message = "登录账号长度不能超过30个字符")
    @TableField(value = "login_name_")
    private String loginName;

    /* 用户名称 */
    @Xss(message = "用户昵称不能包含脚本字符")
    @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
    @TableField(value = "user_name_")
    private String userName;

    /* 用户岗位 */
    @TableField(value = "user_post_")
    private String userPost;

    /* 用户类型 */
    @TableField(value = "user_type_")
    private String userType;

    /* 用户邮箱 */
    @Xss(message = "邮箱不能包含脚本字符")
    @Email(message = "邮箱格式不正确")
    @Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符")
    @TableField(value = "email_")
    private String email;

    /* 手机号码 */
    @Xss(message = "手机号码不能包含脚本字符")
    @Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符")
    @TableField(value = "phonenumber_")
    private String phonenumber;

    /* 用户性别 */
    @TableField(value = "sex_")
    private String sex;

    /* 用户头像 */
    @TableField(value = "avatar_")
    private String avatar;

    /* 密码 */
    @TableField(value = "password_")
    private String password;

    /* 盐加密 */
    @TableField(value = "salt_")
    private String salt;

    /* 帐号状态（0正常 1停用） */
    @TableField(value = "status_")
    private String status;

    /* 删除标志（0代表存在 2代表删除） */
    @TableField(value = "del_flag_")
    private String delFlag;

    /* 最后登录IP */
    @TableField(value = "login_ip_")
    private String loginIp;

    /* 最后登录时间 */
    @TableField(value = "login_date_")
    private Date loginDate;

    /* 密码最后更新时间 */
    @TableField(value = "pwd_update_date_")
    private Date pwdUpdateDate;

    /* 用户协议 */
    @TableField(value = "user_agreement_")
    private String userAgreement;

    /* 部门对象 */
    @TableField(exist = false)
    private SysDept dept;

    /* 用户角色 */
    @TableField(exist = false)
    private List<SysRole> roles;

    /* 用户组 */
    @TableField(exist = false)
    private Long[] userIds;

    /* 角色组 */
    @TableField(exist = false)
    private Long[] roleIds;

    public boolean isAdmin() {
        return isAdmin(this.userId);
    }

    public static boolean isAdmin(Long userId) {
        return userId != null && 1L == userId;
    }


    public SysDept getDept() {
        if (dept == null) {
            dept = new SysDept();
        }
        return dept;
    }


    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("userId", getUserId())
                .append("deptId", getDeptId())
                .append("loginName", getLoginName())
                .append("userName", getUserName())
                .append("userPost", getUserPost())
                .append("userType", getUserType())
                .append("email", getEmail())
                .append("phonenumber", getPhonenumber())
                .append("sex", getSex())
                .append("avatar", getAvatar())
                .append("password", getPassword())
                .append("salt", getSalt())
                .append("status", getStatus())
                .append("delFlag", getDelFlag())
                .append("loginIp", getLoginIp())
                .append("loginDate", getLoginDate())
                .append("createBy", getCreateBy())
                .append("createTime", getCreateTime())
                .append("updateBy", getUpdateBy())
                .append("updateTime", getUpdateTime())
                .append("remark", getRemark())
                .append("dept", getDept())
                .append("roles", getRoles())
                .toString();
    }
}
